package com.nobuyme.box.toy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.bean.AgeListEntity;
import com.nobuyme.box.bean.GoodsListEvent;
import com.nobuyme.box.util.FragmentUtils;
import com.nobuyme.box.util.StrokeTextView;
import com.pachong.android.baseuicomponent.activity.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class ToyListActivity extends BaseActivity {


    @Bind(R.id.stv_top)
    StrokeTextView mStvTop;
    @Bind(R.id.stv_num)
    StrokeTextView mStvNum;
    ArrayList<ImageView> ivList;
    //    @Bind(R.id.lytHotTopic)
//    LinearLayout mLytHotTopic;
//    @Bind(R.id.hsvHotTopic)
//    HorizontalScrollView mHsvHotTopic;
    HorizontalScrollView hsvHotTopic;
    LinearLayout lytHotTopic;
    int position;
    ArrayList<Parcelable> ageListEntity;
    int id;

    public static final String AGELIST_ENTITY="AgeListEntity";
    public static final String ID="ID";
    public static final String POSITION="position";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
        hideToolbar();
        initAdress();
    }
    private void initAdress() {
        final Account acc = AccountManager.getCurrentAccount();
        mStvTop.setText(acc.getName());
        mStvNum.setText(acc.getToybox_code());
    }
    int i;
    //初始化选择项
    private void initTitle(LayoutInflater inflater, View mainView) {
        hsvHotTopic = (HorizontalScrollView) mainView.findViewById(R.id.hsvHotTopic);
        lytHotTopic = (LinearLayout) mainView.findViewById(R.id.lytHotTopic);
        ivList = new ArrayList<>();
        for (i = 0; i < ageListEntity.size(); i++) {
            View itemView = inflater.inflate(R.layout.top_list_pic, null);
           final ImageView ivSelect = (ImageView) itemView.findViewById(R.id.iv_selcet_all);
            final Button btnSelect = (Button) itemView.findViewById(R.id.btn_select);
            ivList.add(ivSelect);
            TextView tvAgeVer = (TextView) itemView.findViewById(R.id.tv_age_ver);
            AgeListEntity o = (AgeListEntity) ageListEntity.get(i);
            if(position==i){
                ivSelect.setVisibility(View.VISIBLE);
                int id = o.getId();
                EventBus.getDefault().post(new GoodsListEvent(id));
            }
            btnSelect.setTag(i);
            tvAgeVer.setText(o.getName());
            lytHotTopic.addView(itemView);
            btnSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int tag = (int) v.getTag();
                    for (int m=0;m<ivList.size();m++){
                            ivList.get(m).setVisibility(View.INVISIBLE);
                            if(tag==m){
                                ivList.get(m).setVisibility(View.VISIBLE);
                                AgeListEntity o = (AgeListEntity) ageListEntity.get(m);
                                int id = o.getId();
                                EventBus.getDefault().post(new GoodsListEvent(id));
                            }
                    }
                }
            });
        }
    }


    public static void start(Activity activity, List<AgeListEntity> o, int id,int position) {
        Intent intent = new Intent(activity, ToyListActivity.class);
        intent.putExtra(AGELIST_ENTITY, (Serializable) o);
        intent.putExtra(ID,id);
        intent.putExtra(POSITION,position);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_toy_list, null);
        ageListEntity = getIntent().getParcelableArrayListExtra(AGELIST_ENTITY);
        id = getIntent().getIntExtra(ID,-1);
        position = getIntent().getIntExtra(POSITION, -1);
        initTitle(inflater, view);
        ToyListFragment toyListFragment = new ToyListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ID, id);
        bundle.putInt(POSITION, position);
        toyListFragment.setArguments(bundle);
        FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fl_rent_detail, toyListFragment);
        return view;
    }

//
    @OnClick(R.id.btn_list_back)
    public void onViewClicked() {
        finish();
    }
}
